package zzhyc.keystore

import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.activity.OnBackPressedCallback
import androidx.appcompat.app.AppCompatActivity
import com.kwad.sdk.api.KsAdSDK
import com.kwad.sdk.api.KsDrawAd
import com.kwad.sdk.api.KsLoadManager
import com.kwad.sdk.api.KsScene
import zzhyc.keystore.databinding.ActivityShouKuaiDrawBinding

private const val TAG = "ShouKuaiDrawActivity"

class ShouKuaiDrawActivity : AppCompatActivity() {

    private val binding by lazy { ActivityShouKuaiDrawBinding.inflate(layoutInflater) }
    private var logStr = StringBuilder()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)

        binding.ksDrawTvBack.setOnClickListener {
            setIntent()
            finish()
        }

        initDraw()

        onBackPressedDispatcher.addCallback(this, object : OnBackPressedCallback(true) {
            override fun handleOnBackPressed() {
                Log.i(TAG, "onBackPressedDispatcher")
                setIntent()
                finish()
            }
        })
    }

    private fun setIntent() {
        val returnIntent = Intent()
        returnIntent.putExtra("drawLogs", logStr.toString())
        setResult(RESULT_OK, returnIntent)
    }

    private fun initDraw() {
        val ksScene = KsScene.Builder(ShouKuaiKey.KS_DRAW_ID).build()
        KsAdSDK.getLoadManager().loadDrawAd(ksScene, object : KsLoadManager.DrawAdListener {
            override fun onError(p0: Int, p1: String?) {
                setLogsView("快手draw信息流/onError: ${p0}，msg-- ${p1} \n")
            }

            override fun onDrawAdLoad(p0: MutableList<KsDrawAd>?) {
                setLogsView("快手draw信息流/onDrawAdLoad: \n")
                if (p0 == null || p0.isEmpty()) return
                val drawView = p0[0]
                drawView.setAdInteractionListener(object : KsDrawAd.AdInteractionListener {
                    override fun onAdClicked() {
                        setLogsView(
                            "快手draw信息流/onAdClicked: 广告点击回调，价格为-- ${drawView.ecpm} 分 \n"
                        )
                    }

                    override fun onAdShow() {
                        setLogsView("快手draw信息流/onAdShow: 广告曝光回调 \n")
                    }

                    override fun onVideoPlayStart() {
                        setLogsView("快手draw信息流/onVideoPlayStart: 广告视频开始播放 \n")
                    }

                    override fun onVideoPlayPause() {
                        setLogsView("快手draw信息流/onVideoPlayPause: 广告视频暂停播放 \n")
                    }

                    override fun onVideoPlayResume() {
                        setLogsView("快手draw信息流/onVideoPlayResume: 广告视频恢复播放 \n")
                    }

                    override fun onVideoPlayEnd() {
                        setLogsView(
                            "快手draw信息流/onVideoPlayEnd: 广告视频播放结束/价格为-- ${drawView.ecpm} 分 \n"
                        )
                    }

                    override fun onVideoPlayError() {
                        setLogsView("快手draw信息流/onVideoPlayError: 广告视频播放出错 \n")
                    }
                })

                val drawVideoView = drawView.getDrawView(this@ShouKuaiDrawActivity)
                if (drawVideoView != null && drawVideoView.getParent() == null) {
                    binding.ksDrawFl.removeAllViews()
                    binding.ksDrawFl.addView(drawVideoView)
                }
            }
        })
    }

    private fun setLogsView(logs: String) {
        Log.i(TAG, logs)
        logStr.append(logs)
    }

    override fun onDestroy() {
        super.onDestroy()
        if (logStr.isNotEmpty()) {
            logStr.setLength(0)
        }
    }
}